<template>
  <div style="width: 100%;background: #FFFFFF;">
    <div class="security-check-box">
      <div class="echart">
        <p class="security-title">安全检查总次数</p>
        <div ref="security-check-chart" class="security-check-chart"></div>
      </div>

      <div class="security-check-message">
        <p class="security-title">安全检查未整改警示</p>
        <div class="security-warming-label-wrapper">
          <div class="security-warming-label">
            <span class="dot red"></span>
            <span class="security-warming-label-text">一级警示</span>
          </div>
          <div class="security-warming-label">
            <span class="dot yellow"></span>
            <span class="security-warming-label-text">二级警示</span>
          </div>
        </div>
        <ul class="security-warming-content">
          <li class="security-warming-message" v-for="(item,index) in WarmingList" :key="index">
            <span class="dot" :class="securityLevel(item)"></span>
            <span>
              {{item.message}}
            </span>
          </li>
        </ul>
      </div>
    </div>
  </div>

</template>

<script>
  import * as echarts from 'echarts';
  export default {
    name: "SecurityDataInfo",
    data() {
      return {
        myChart:null,
        bgColor: '#fff',
        title: '总次数（次）',
        color: ['#177DDC', '#2CB96D', '#FB568A', '#FFBB34'],
        echartData: [{
            name: "一次通过",
            value: "1007"
          },
          {
            name: "一次整改",
            value: "370"
          },
          {
            name: "多次整改",
            value: "307"
          },
          {
            name: "未整改",
            value: "330"
          }
        ],
        WarmingList: [{
            message: '这是一条安全未检查未整改警示信息（警示等级为2级）这是一条安全为检查未整改警示信息',
            grade: 2,
          },
          {
            message: '这是一条安全未检查未整改警示信息（警示等级为2级）这是一条安全为检查未整改警示信息',
            grade: 2,
          },
          {
            message: '这是一条安全未检查未整改警示信息（警示等级为1级）这是一条安全为检查未整改警示信息',
            grade: 1,
          },
          {
            message: '这是一条安全未检查未整改警示信息（警示等级为1级）这是一条安全为检查未整改警示信息',
            grade: 1,
          },
          {
            message: '这是一条安全未检查未整改警示信息（警示等级为1级）这是一条安全为检查未整改警示信息',
            grade: 1,
          },
        ],
        levelMap: {
          1: 'red',
          2: 'yellow'
        },
      }
    },
    computed:{
      option(){
        const {echartData} = this;
        const total =  echartData.reduce((a, b) => (a + b.value * 1), 0)
        return {
          width: '450px',
          height: '400px',
          color: ['#177DDC', '#2CB96D', '#FB568A', '#FFBB34'],
          title: [{
            text: '{name|' + this.formatNumber(total) + '}\n{val|总次数（次）}',
            top: 'center',
            left: 'center',
            textStyle: {
              rich: {
                name: {
                  fontSize: 20,
                  fontWeight: 'bold',
                  color: ' #333333',
                  padding: [10, 0]
                },
                val: {
                  fontSize: 14,
                  fontWeight: 'normal',
                  color: '#222222',
                }
              }
            }
          }, {
            top: 10,
            left: 38,
            textStyle: {
              fontSize: 20,
              color: '#333333',
              fontWeight: 500
            }
          }],
          legend: {
            orient: 'horizontal',
            icon: 'circle',
            bottom:'20',
            itemWidth: 10,
            itemHeight: 10,
            itemGap: 31,
            textStyle: {
              rich: {
                name: {
                  fontSize: 14
                },
              }
            },
          },
          series: [{
            type: 'pie',
            radius: ['50%', '35%'],
            center: ['64%', '35%'],
            data: echartData,
            itemStyle: {
              normal: {
                borderWidth: 0
              }
            },
            labelLine: {
              normal: {
                length: 20,
                length2: 50,
                lineStyle: {
                  color: '#e6e6e6'
                }
              }
            },
            label: {
              normal: {
                formatter: params => this.toPercent(params.value,total),
                padding: [0, -10, -5, -10],
                fontSize: 20,
              }
            },
          }]
        }
      }
    },
    mounted(){
      this.initChart()
      this.setOption()
    },
    methods: {
      securityLevel(item) {
        return this.levelMap[item.grade] || ''
      },
      toPercent(num,total) {
        let a = Number((num / total) * 100).toFixed(1);
        a += "%";
        return a;
      },
      formatNumber(num) {
        let reg = /(?=(\B)(\d{3})+$)/g;
        return num.toString().replace(reg, ',');
      },
      initChart(){
        this.myChart = echarts.init(this.$refs['security-check-chart']);
      },
      setOption(){
        this.myChart && this.myChart.setOption(this.option)
      }
    }
  };
</script>

<style lang="less" scoped>
  .echart {
    width: 50%;
  }
  .security-title {
    font-weight: bold;
    font-size: 16px;
  }
  .security-check-box {
    display: flex;
    // width: 62.5%;
    margin-top: 20px;
    justify-content: space-between;
    .dot {
      width: 10px;
      height: 10px;
      border-radius: 50%;
      display: inline-block;
      margin-right: 10px;

      &.red {
        background: #D70C18;
      }

      &.yellow {
        background: #FFBB34;
      }
    }

    .security-check-chart {
      width: 100%;
      height: 290px;
    }

    .security-check-message {
      width: 50%;
      height: 320px;
      /*padding-top: 40px;*/
      /*padding-left: 42px;*/

      .security-warming-title {
        display: inline-block;
        height: 20px;
        font-size: 16px;
        font-weight: 500;
      }

      .security-warming-content {
        list-style: none;
        padding-right: 54px;
      }

      .security-warming-content li {
        font-size: 14px;
        color: rgb(112, 112, 112);
        line-height: 14px;
        font-weight: bold;
        margin-top: 40px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }

    }

    .security-warming-label-wrapper {
      /*line-height: 20px;*/
      float: right;
      display: inline-block;
      margin-right: 45px;
      font-size: 14px;
      color: rgb(112, 112, 112);
      font-weight: bold;
    }

    .security-warming-label {
      display: inline-block;

      &+.security-warming-label {
        margin-left: 20px;
      }

    }
  }

</style>
